package flc.ast.fragment;

import androidx.fragment.app.Fragment;

import android.app.Dialog;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;

import flc.ast.R;
import flc.ast.activity.PlayLinkActivity;
import flc.ast.activity.PriAlbumActivity;
import flc.ast.activity.PubAlbumActivity;
import flc.ast.activity.TakeVideoActivity;
import flc.ast.activity.PlayLocalActivity;
import flc.ast.activity.SelVideoActivity;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.SPUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private int position = 0;//选择的密保问题角标
    private Dialog mySetPasswordDialog;
    private EditText dialogSetPass, dialogSetPassAgain, dialogSetAnswer;
    private Dialog myInputPasswordDialog;
    private EditText dialogInputPass;
    private Dialog myForgetPasswordDialog;
    private EditText dialogForgetAnswer, dialogForgetPass, dialogForgetPassAgain;
    private String[] SecurityStr;


    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        mDataBinding.ivTakeVideo.setOnClickListener(this);
        mDataBinding.ivLocalPlay.setOnClickListener(this);
        mDataBinding.ivFormatPlay.setOnClickListener(this);
        mDataBinding.ivLinkPlay.setOnClickListener(this);
        mDataBinding.ivPubAlbum.setOnClickListener(this);
        mDataBinding.ivPriAlbum.setOnClickListener(this);
        //密保问题
        SecurityStr = new String[]{getString(R.string.you_home), getString(R.string.you_birthday), getString(R.string.you_school)};
        setPasswordDialog();
        inputPasswordDialog();
        forgetPasswordDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivTakeVideo:
                startActivity(TakeVideoActivity.class);
                break;
            case R.id.ivLocalPlay:
                startActivity(PlayLocalActivity.class);
                break;
            case R.id.ivFormatPlay:
                SelVideoActivity.type = 0;
                startActivity(SelVideoActivity.class);
                break;
            case R.id.ivLinkPlay:
                startActivity(PlayLinkActivity.class);
                break;
            case R.id.ivPubAlbum:
                startActivity(PubAlbumActivity.class);
                break;
            case R.id.ivPriAlbum:
                boolean isHave = SPUtil.getBoolean(mContext, Content.isHavePassword, false);
                if (isHave) {
                    dialogInputPass.setText("");
                    myInputPasswordDialog.show();
                } else {
                    dialogSetAnswer.setHint(SecurityStr[position]);//默认密保
                    mySetPasswordDialog.show();
                }
                break;
            //设置密码弹框----------------------------------
            case R.id.ivDialogSetSwitch://切换密保问题
                if (position == 0) {
                    position++;
                } else if (position == 1) {
                    position++;
                } else if (position == 2) {
                    position = 0;
                }
                dialogSetAnswer.setHint(SecurityStr[position]);
                break;
            case R.id.ivDialogSetCancel://取消
                mySetPasswordDialog.dismiss();
                break;
            case R.id.ivDialogSetRight://确定
                if (TextUtils.isEmpty(dialogSetPass.getText().toString()) || dialogSetPass.getText().length() < 6) {
                    ToastUtils.showShort(R.string.please_input_6_password);//为空或者小于6
                } else if (TextUtils.isEmpty(dialogSetPassAgain.getText().toString()) || dialogSetPassAgain.getText().length() < 6) {
                    ToastUtils.showShort(R.string.please_right_new_password);//为空或者小于6
                } else if (!dialogSetPass.getText().toString().equals(dialogSetPassAgain.getText().toString())) {
                    ToastUtils.showShort(R.string.two_password_no_equest);//两次密码不一致
                } else if (TextUtils.isEmpty(dialogSetAnswer.getText().toString())) {
                    ToastUtils.showShort(R.string.please_input_password_answer);//密保答案为空
                } else {
                    SPUtil.putBoolean(mContext, Content.isHavePassword, true);
                    SPUtil.putString(mContext, Content.myPassword, dialogSetPass.getText().toString());
                    SPUtil.putString(mContext, Content.myProblem, SecurityStr[position]);
                    SPUtil.putString(mContext, Content.myAnswer, dialogSetAnswer.getText().toString());
                    ToastUtils.showShort(R.string.set_password_suc);
                    mySetPasswordDialog.dismiss();
                }
                break;
            //输入密码弹框----------------------------------
            case R.id.ivDialogInputCancel://取消
                myInputPasswordDialog.dismiss();
                break;
            case R.id.ivDialogInputRight://确定
                String password = SPUtil.getString(mContext, Content.myPassword, "");
                if (TextUtils.isEmpty(dialogInputPass.getText().toString())) {
                    ToastUtils.showShort(R.string.please_input_6_password);
                } else if (!dialogInputPass.getText().toString().equals(password)) {
                    ToastUtils.showShort(R.string.password_def);
                } else {
                    myInputPasswordDialog.dismiss();
                    //去到私密相册
                    startActivity(PriAlbumActivity.class);
                }
                break;
            case R.id.tvDialogInputForget://忘记密码
                myInputPasswordDialog.dismiss();
                dialogForgetAnswer.setText("");
                dialogForgetAnswer.setHint(SPUtil.getString(mContext, Content.myProblem, ""));
                dialogForgetPass.setText("");
                dialogForgetPassAgain.setText("");
                myForgetPasswordDialog.show();
                break;
            //忘记密码弹框----------------------------------
            case R.id.ivDialogForgetCancel://取消
                myForgetPasswordDialog.dismiss();
                break;
            case R.id.ivDialogForgetRight://确认
                if (TextUtils.isEmpty(dialogForgetAnswer.getText().toString())) {
                    ToastUtils.showShort(R.string.please_input_password_answer);
                } else if (TextUtils.isEmpty(dialogForgetPass.getText().toString()) || dialogForgetPass.getText().length() < 6) {
                    ToastUtils.showShort(R.string.please_input_6_password);
                } else if (TextUtils.isEmpty(dialogForgetPassAgain.getText().toString()) || dialogForgetPassAgain.getText().length() < 6) {
                    ToastUtils.showShort(R.string.again_input_6_password);
                } else if (!dialogForgetPass.getText().toString().equals(dialogForgetPassAgain.getText().toString())) {
                    ToastUtils.showShort(R.string.two_password_no_equest);
                } else if (!dialogForgetAnswer.getText().toString().equals(SPUtil.getString(mContext, Content.myAnswer, ""))) {
                    ToastUtils.showShort(R.string.answer_def);
                } else {
                    SPUtil.putString(mContext, Content.myPassword, dialogForgetPass.getText().toString());
                    ToastUtils.showShort(R.string.update_password_suc);
                    myForgetPasswordDialog.dismiss();
                }
                break;
        }
    }

    @Override
    protected void initData() {

    }

    //设置密码弹框
    private void setPasswordDialog() {
        mySetPasswordDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_set_password, null);
        mySetPasswordDialog.setContentView(inflate);
        mySetPasswordDialog.setCancelable(false);
        //获取当前Activity所在的窗体
        Window dialogWindow = mySetPasswordDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView dialogClose = inflate.findViewById(R.id.ivDialogSetCancel);
        ImageView dialogSwitch = inflate.findViewById(R.id.ivDialogSetSwitch);
        ImageView dialogCom = inflate.findViewById(R.id.ivDialogSetRight);
        dialogClose.setOnClickListener(this);
        dialogSwitch.setOnClickListener(this);
        dialogCom.setOnClickListener(this);
        dialogSetPass = inflate.findViewById(R.id.etDialogSetPassword);
        dialogSetPassAgain = inflate.findViewById(R.id.etDialogSetAgain);
        dialogSetAnswer = inflate.findViewById(R.id.etDialogSetAnswer);
    }

    //输入密码弹框
    private void inputPasswordDialog() {
        myInputPasswordDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_input_password, null);
        myInputPasswordDialog.setContentView(inflate);
        myInputPasswordDialog.setCancelable(false);
        //获取当前Activity所在的窗体
        Window dialogWindow = myInputPasswordDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView dialogClose = inflate.findViewById(R.id.ivDialogInputCancel);
        TextView dialogForget = inflate.findViewById(R.id.tvDialogInputForget);
        ImageView dialogCom = inflate.findViewById(R.id.ivDialogInputRight);
        dialogClose.setOnClickListener(this);
        dialogCom.setOnClickListener(this);
        dialogForget.setOnClickListener(this);
        dialogInputPass = inflate.findViewById(R.id.etDialogInputPassword);
    }

    //修改密码弹框
    private void forgetPasswordDialog() {
        myForgetPasswordDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_forget_password, null);
        myForgetPasswordDialog.setContentView(inflate);
        myForgetPasswordDialog.setCancelable(false);
        //获取当前Activity所在的窗体
        Window dialogWindow = myForgetPasswordDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView dialogClose = inflate.findViewById(R.id.ivDialogForgetCancel);
        ImageView dialogCom = inflate.findViewById(R.id.ivDialogForgetRight);
        dialogClose.setOnClickListener(this);
        dialogCom.setOnClickListener(this);
        dialogForgetAnswer = inflate.findViewById(R.id.etDialogForgetAnswer);
        dialogForgetPass = inflate.findViewById(R.id.etDialogForgetPassword);
        dialogForgetPassAgain = inflate.findViewById(R.id.etDialogForgetAgain);
    }

}